package com.web.kdl.controller.site;

import com.web.kdl.common.result.CommonResult;
import com.web.kdl.component.WebSocketService;
import com.web.kdl.entity.site.Site;
import com.web.kdl.pojo.Page;
import com.web.kdl.pojo.form.SiteAddForm;
import com.web.kdl.pojo.form.SiteUpdateForm;
import com.web.kdl.service.createTable.factory.TableFactory;
import com.web.kdl.service.site.SiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author： shulibin
 * @Date： 2025/7/24 17:00
 * @Describe：
 */
@RestController
@RequestMapping("/site")
public class SiteController {

    @Resource
    SiteService siteService;
    @Autowired
    WebSocketService webSocketService;
//  添加站点 与站点相关的表哪些：site_wheel  site_info
    @PostMapping("/addSite")
    public CommonResult addSite(@Valid @RequestBody SiteAddForm form){
        int i = siteService.addSite(form);
        if (i == 0){
            return CommonResult.error("该站点已存在");
        }
        webSocketService.sendTextMsg("/response/addSite",form);
        return CommonResult.success("站点添加成功");
    }

    //  删除站点
    @DeleteMapping("/deleteSite")
    public CommonResult deleteSite(@NotNull String id){
        try {
            return siteService.deleteById(id);
        } catch (Exception e) {
            return CommonResult.error("删除站点时出错: " + e.getMessage());
        }
    }
//  删除站点
    @DeleteMapping("/deleteSites")
    public CommonResult deleteSites(@RequestBody List<String> siteIds){
// 1. 参数校验
        if (siteIds == null || siteIds.isEmpty()) {
            return CommonResult.error("请提供要删除的用户ID列表");
        }
        // 2. 执行批量删除
        try {
            for (String siteId : siteIds) {
                siteService.deleteById(siteId);
            }
        } catch (Exception e) {
            return CommonResult.error("删除站点时出错: " + e.getMessage());
        }
        return CommonResult.success("删除成功");
    }

    //  查询站点
    @GetMapping("/getAllSite")
    public CommonResult getAllSite(){
        List<Site> allSite = siteService.getAllSize();
        return CommonResult.success(allSite);
    }

    // TODO 查询分页站点
    @PostMapping("/getSiteByPage/")
    public CommonResult getSiteByPage(Page page){
        return CommonResult.success();
    }

//  修改站点
    @PostMapping("/updateSite")
    public CommonResult updateSite(@RequestBody SiteUpdateForm form){
        Boolean res = siteService.updateSite(form);
        return res?CommonResult.success("站点更新成功"):CommonResult.error("站点更新失败");
    }



}
